;define(function (require) {
	'use strict';
	var $=require('jquery');
	var logs=require('plugin/logs');
	var echart3=require('plugin/echarts.min');
		require('plugin/superslide');
	var config = require('config');
	var $Fun = require('functions');
	
	var reqConfig = {};
	if(config.debugModel){
		reqConfig = config.debugAjaxRequestUrls;//测试数据
	}else{
		reqConfig = config.urlConfig;//接口配置
	}
	
	
	//地图工具
	function mapMaker(){};
	mapMaker.prototype = {
		init:function(){
			this.data = {};
			return this;
		}
	}
	var mapMakerObj = new mapMaker().init();
	
	var app = {};
	//初始化
	app.init = function(){
		//十六进制转rgba
		String.prototype.color2Rgba=function(){var sColor=this.toLowerCase(),reg=/^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;if(sColor&&reg.test(sColor)){if(sColor.length===4){var sColorNew="#";for(var i=1;i<4;i+=1){sColorNew+=sColor.slice(i,i+1).concat(sColor.slice(i,i+1))}sColor=sColorNew}var sColorChange=[];for(var i=1;i<7;i+=2){sColorChange.push(parseInt("0x"+sColor.slice(i,i+2)))}return"RGBA("+sColorChange.join(",")+","}else{return sColor}};
		
//		logs.log('loaded.');

		var _nav=require('plugin/nav');
		var nav=new _nav('#navbox','#navpoint');
		nav.run();
		
		//导航菜单
		$('#navbox li.menu').click(function(e){
			var el = window.event.srcElement || e.target;
			if(el.nodeName=="LI"){
				if($(this).find('a').length>0 && $(this).find('a').attr('href').indexOf('javascript')<0 ){
					location.href = $(this).find('a').attr('href'); 
				}
			}
		})
		
		//仪表盘
		$('.tv-chart-slide').slide({
			mainCell:".bd ul",
			effect:"left",
			vis:1,
			defaultIndex:1,
			pnLoop:false
			,startFun:function(index, page, $slide, $hd, $bd){
				var $act = $bd.find('li').eq(index);
				$act.addClass('active').siblings('li').removeClass('active');
			}
		});
		
		//缓存数据
		//_cache['url1-2015-09']
		//首页>经济监测
		_cache['url1-time'] = '2015-09';
		//这里有ajax请求
		$Fun.setCacheData( reqConfig.url1+'?year=2015&month=09', 'url1-'+_cache['url1-time'] );
		
		//图表生成
		//五个球
		$Fun.drawChart1( '.tv-chart-a', _cache['url1-'+_cache['url1-time']].g1 );
		$Fun.drawChart1( '.tv-chart-b', _cache['url1-'+_cache['url1-time']].g2 );
		$Fun.drawChart1( '.tv-chart-c', _cache['url1-'+_cache['url1-time']].g3 );
		$Fun.drawChart1( '.tv-chart-d', _cache['url1-'+_cache['url1-time']].g4 );
		$Fun.drawChart1( '.tv-chart-e', _cache['url1-'+_cache['url1-time']].g5 );

		//首页>统计报表
		_cache['url2-time'] = '2015-06';
		$Fun.setCacheData( reqConfig.url2+'?year=2015&month=06', 'url2-'+_cache['url2-time'] );
		
		//创建报表缩略图动画
		$Fun.drawChart2( _cache['url2-'+_cache['url2-time']], '2015' );

		//首页>统计地图>指标类型-点击切换
		$('.JQ_zbTab').slide({
			titCell:'.head li',
			trigger:'click'
		});
		
		//地图根据指标初始化
		function mapInit(params){
			var data = params.data;
			
			//测试随机数据-----start
			//console.log($Fun.getRandomNum());
			//测正式数据时把config中debugModel关闭
			if(config.debugModel){
				//重组随机数据
				for(var i=0,l=data.areaData.length;i<l;i++){
					data.areaData[i].value = $Fun.getRandomNum(80,550);
				}
			}//测试数据-----end
			
			//缓存起来
			mapMakerObj.zbType = data;//指标类型
			
			$Fun.drawChartMap( '#mapJs', data, function(rtn){
				//获取地图实例
				var echartIsc = echart3.getInstanceByDom($('#mapJs')[0]);
				//更新数据
				var option = rtn;
				option.tooltip.show = true;
				echartIsc.setOption(option, true);
			})
		}
		
		//指标点击事件
		$('.JQ_zbTab .head li').click(function(){
			//TODO:获取接口数据
			var jsons = {
				"Data":{
					"areaData":
						[{"name":"苏州市","value":545.4},
						{"name":"南京市","value":337.8},
						{"name":"无锡市","value":274.9},
						{"name":"南通市","value":189},
						{"name":"徐州市","value":168.8},
						{"name":"盐城市","value":161.1},
						{"name":"常州市","value":145.6},
						{"name":"淮安市","value":120},
						{"name":"宿迁市","value":107.1},
						{"name":"泰州市","value":104.1},
						{"name":"连云港市","value":100.9},
						{"name":"镇江市","value":94.1},
						{"name":"扬州市","value":83.7}],
					"colors":["#006ecb"],
					"map":"jiangsu",
					"mapName":"江苏",
					"max":550,
					"min":80},
				"Success":true
			};
			
			//缓存指标名称
			mapMakerObj.zbText = $(this).text();
			
			//更新地图
			mapInit({
				data:jsons.Data
			});
		})
		
		//首次运行初始化注册地图组件
		$Fun.getJsonData(config.mapConfig.urlJsMap, function(returnJson){
			echart3.registerMap('jiangsu', returnJson);
			var mapChart = echart3.init($('#mapJs')[0]);
			//触发点击事件更新地图
			$('.JQ_zbTab .head li:eq(0)').trigger('click');
		})
		
		/*---------------model2start------------*/
		//首页>统计地图>指标类型、指标排名、指标占比三个标签点击切换
		$('.JQ_pmTab').slide({
			titCell:'.hd ul li',
			mainCell:'.bd ul',
			trigger:'click'
		});
		//标签切换事件
		$('.JQ_pmTab .hd ul li').click(function(){
			//读取第一个指标缓存的数据
			var data = mapMakerObj.zbType;
			//读取缓存的指标名称
			$('.JS_zbTitle').text(mapMakerObj.zbText);
			//绘图
			zbPaiMingFun(data);
		})

		//指标排名
		function zbPaiMingFun(data){
			if(data && data.areaData && data.areaData.length>0){
				//数据重组
				var arr = [];
				var names = [];
				var pmTotal = 0;
				$.each(data.areaData, function(i,e) {
					names.push(e.name);
					var v = parseFloat(e.value);
					arr.push(v);
					pmTotal += v;
				});
				var pmData = arr;
				var dataPm = { names:names, pmData:pmData, max:data.max, pmTotal:pmTotal };
				//排名柱状图生成
				$Fun.drawChartPaiMing( '#pmChart', dataPm, function(rtn){
					var option = rtn;
					var pmChart = echart3.init($('#pmChart')[0]);
					pmChart.setOption(option, true);
					
					//同时更新占比图
					zbZhanBi(data,dataPm);
				} );
			}else{
				echart3.getInstanceByDom($('#pmChart')[0]).clear();
			}
		}
		
		/*---------------model2end------------*/
		
		/*----------------model3start--------------*/
		//占比图
		function zbZhanBi(data,dataPm){
			var clArr = $Fun.getOpacityColors(data.colors[0].color2Rgba());
			var dataZb = $.extend(true, data, dataPm);
			//读取缓存的指标名称
			dataZb.name = mapMakerObj.zbText;
			$Fun.drawChartZhanBi( '#zbChart', clArr, dataZb, function(rtn){
				var option = rtn;
				var zbChart = echart3.init($('#zbChart')[0]);
				zbChart.setOption(option, true);
			})
		}
		/*------------model3end----------*/
		
		//添加参数变化监听事件
		$('.JQ_mapTime').change(function(){
			//TODO:日期条件变化
		})
		
	};
	return app;
});